package com.hrt.manage.order.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.hrt.base.BaseController;
import com.hrt.bean.GiftDetail;
import com.hrt.bean.GiftInfor;
import com.hrt.bean.OrderDetail;
import com.hrt.bean.OrderInfor;
import com.hrt.manage.order.service.OrderDetailService;
import com.hrt.manage.order.service.OrderInforService;
import com.hrt.manage.product.service.GiftDetailService;
import com.hrt.manage.product.service.GiftInforService;
@Controller
@RequestMapping("manage/order/orderDetail")
public class OrderDetailController extends BaseController<OrderDetail>{
	@Autowired
	private OrderDetailService orderDetailService;
	@Autowired
	private OrderInforService orderInforService;
	@Autowired
	private GiftDetailService giftDetailService;
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/queryByPage")
	@ResponseBody
	public Map queryByPage(
			@RequestParam(value = "pageIndex", required = false) Integer pageIndex,
			@RequestParam(value = "pageSize", required = false) Integer pageSize,
			HttpServletRequest request) {
		Map resultMap = new HashMap();

		if (pageSize == null || "".equals(pageSize)) {
			pageSize = 30;
		}
		if (pageIndex == null || "".equals(pageIndex)) {
			pageIndex = 1;
		}
		setPageIndex(pageIndex);

		setPageSize(pageSize);

		setTotalCount(orderDetailService.totalCount());

		List<OrderDetail> resultList = orderDetailService.queryByPage(
				getPageIndex(), getPageSize());
		setList(resultList);

		this.setPageCount(this.computePageCount(this.getTotalCount(),
				this.getPageSize()));

		page = this.getPage();

		resultMap.put("pageResult", page);
		return resultMap;
	}
	
	

	/**
	 * 
	 * 
	 * @param request
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/list")
	public ModelAndView list(HttpServletRequest request) {
		ModelAndView view = new ModelAndView();
		
			list = orderDetailService.queryByPage(getPageIndex(), getPageSize());
			setTotalCount(orderDetailService.totalCount());
			this.setPageCount(this.computePageCount(this.getTotalCount(),
					this.getPageSize()));
			
			page = this.getPage();
			view.addObject("pageResult", page);
		
		return view;

	}
	/**
	 * 根据orderId查找订单详情
	 * 
	 * @param orderId
	 * @param request
	 * @param response
	 * @return 订单详情集合
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/queryOrderDetail")
	@ResponseBody
	public Map queryOrderDetail(
			@RequestParam(value = "id", required = true) Long orderId,
			HttpServletRequest request, HttpServletResponse response) {
		Map resultMap = new HashMap();

		List<OrderDetail> resultList = orderDetailService.getOrderDetailByOrderId(orderId);
		resultMap.put("resultList", resultList);
		return resultMap;

	}

	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public @ResponseBody Long[] delete(
			@RequestParam(value = "ids", required = true) String ids,
			HttpServletResponse response) throws IOException {
		Long[] idArr = null;

		if (ids != null) {
			if (ids.indexOf(",") != -1) {
				String[] idStrs = ids.split(",");
				idArr = new Long[idStrs.length];
				for (int idx = 0; idx < idStrs.length; idx++) {
					idArr[idx] = Long.parseLong(idStrs[idx]);
				}
			} else {
				idArr = new Long[] { Long.parseLong(ids) };
			}

		}
		try {
			orderDetailService.deleteById(idArr);

			return idArr;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	}

	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public void save(@ModelAttribute("orderDetail") OrderDetail orderDetail,
	HttpServletRequest request, HttpServletResponse response) {

		System.out.println("save");
		try {
			OrderInfor orderInfor = this.orderInforService.getOrderByid(new Long(
					orderDetail.getOrderId()));
			GiftDetail giftDetail = this.giftDetailService.getById(new Long(
					orderDetail.getGiftDetailId()));
			
			orderDetail.setOrderInfor(orderInfor);
			orderDetail.setGiftDetail(giftDetail);
			orderDetailService.saveOrderDetail(orderDetail);

			response.sendRedirect("list.do");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	

	@RequestMapping(value = "/show")
	@ResponseBody
	public Map show(
			@RequestParam(value = "id", required = true) String orderDetailId,
			HttpServletRequest request, HttpServletResponse response) {

		Map map = new HashMap();
		if (orderDetailId != null && !"".equals(orderDetailId)) {
			System.out.println("showshowshowshowshow " + orderDetailId);
			OrderDetail orderDetail = this.orderDetailService.getOrderDetailByid(new Long(orderDetailId));
			if (orderDetail != null) {
				map = transBean2Map(orderDetail);
			}

		}
		return map;

	}

	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public void update(@ModelAttribute("orderDetail") OrderDetail orderDetail,
			HttpServletRequest request, HttpServletResponse response) {
		System.out.println("update");

		try {
			OrderInfor orderInfor = this.orderInforService.getOrderByid(new Long(
					orderDetail.getOrderId()));
			GiftDetail giftDetail = this.giftDetailService.getById(new Long(
					orderDetail.getGiftDetailId()));
			
			orderDetail.setOrderInfor(orderInfor);
			orderDetail.setGiftDetail(giftDetail);
			orderDetailService.updateOrderDetail(orderDetail);
			response.sendRedirect("list.do");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
